<layout:basic/>
<script src="__PUBLIC__/assets/plugins/layui/layui.js"></script>
<component:application.module.admin.library.component.Datatable id="actionlist" title="方法列表" width="12">
    <button id="refreshmenu" class="btn btn-default"><i class="fa fa-refresh"></i> 刷新列表</button>
</component:application.module.admin.library.component.Datatable>

<script>
    isea.ready(function () {
        layui.use('layer', function () {
            var layer = layui.layer;
            isea.loader.use("datatables", function () {
                isea.datatables.solve(function () {
                    var actionlist = isea.datatables.create("#actionlist", {
                        config: {
                            ordering: true,
                            language: {
                                emptyTable: '没有数据',
                                loadingRecords: '加载中...',
                                processing: '查询中...',
                                search: '检索:',
                                lengthMenu: '每页 _MENU_ 记录',
                                zeroRecords: '没有数据',
                                paginate: {
                                    first: '<<',
                                    last: '>>',
                                    next: '>',
                                    previous: '<'
                                },
                                info: '第 _PAGE_ 页 / 总 _PAGES_ 页',
                                infoEmpty: '没有数据',
                                infoFiltered: '(过滤总数 _MAX_ 记录)'
                            },
                            columns: [
                                {
                                    title: "ID",
                                    data: "id",
                                    width: "5%"
                                },
                                {
                                    title: "模块",
                                    data: "module",
                                    width: "12%"
                                },
                                {
                                    title: "控制器",
                                    data: "controller",
                                    width: "8%"
                                },
                                {
                                    title: "操作",
                                    data: "method",
                                    width: "12%"
                                },
                                {
                                    title: "路径",
                                    data: "path",
                                    width: "32%"
                                },
                                {
                                    title: "添加时间",
                                    data: "add_time"
                                },
                                {
                                    title: "备注",
                                    data: "comment",
                                    "class": "show_prompt"
                                },
                                {
                                    title: "操作",
                                    data: function (row) {
                                        var html = "";
                                        if (parseInt(row.aid) === 0) {
                                            html = "<button class='btn btn-xs btn-default setaccess'>设置为资源链接</button>";
                                        }
                                        return html;
                                    }
                                }
                            ]
                        }
                    }).onDraw(function () {
                        $(".btn.setaccess").unbind("click").click(function () {
                            var row = $(this).closest("tr");
                            var rowdata = actionlist.data(row);
                            $.get("/admin/access/add", {
                                name: rowdata.path,
                                value: rowdata.path,
                                disable: 1, /* 初始状态为禁用 */
                                comment: rowdata.comment
                            }, function (res) {
                                if (res.status) {
                                    rowdata.aid = res.data.id;
                                    actionlist.update(rowdata, row);
                                }
                            });
                        });
                        $("#actionlist").find("td.show_prompt").unbind("click").click(function () {
                            var row = $(this).closest("tr");
                            var rowdata = actionlist.data(row);

                            layer.prompt({
                                title: '请输入备注',
                                formType: 2,
                                value: rowdata.comment
                            }, function (comment, index) {
                                $.get("__PUBLIC__/admin/reflect/updatecomment", {
                                    id: rowdata.id, comment: comment
                                }, function (res) {
                                    if (res.status) {
                                        rowdata.comment = comment;
                                        actionlist.update(rowdata, row);
                                    }
                                    layer.close(index);
                                    return layer.msg(res.message);
                                });
                            });

                        });
                    });

                    $.get("__PUBLIC__/admin/reflect/getlist", function (res) {
                        if (res) {
                            actionlist.load(res.data);
                        } else {
                            return layer.msg(res.message);
                        }
                    });
                    $("#refreshmenu").click(function () {
                        $.get("__PUBLIC__/admin/reflect/refresh", function (res) {
                            if (res) {
                                actionlist.load(res.data);
                            } else {
                                return layer.msg(res.message);
                            }
                        });
                    });

                });
            });
        });
    });
</script>